package com.kasa.gps;

import java.util.List;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Paint.Style;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.FrameLayout;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.kasa.gps.db.table.LocationInfoTableAdapter;
import com.kasa.gps.tool.Tool;

/**
 * 在google 地图上显示当前位置
 * */
public class OrderGPSMapActivity extends MapActivity {

	private FrameLayout mapLayout = null;
	private static MapView mapView = null;
	private static MapController mapCtr = null;
	private static GeoPoint geoPoint = null;
	private static MyOverLay myol  = null;
	private static boolean canUpdate = false;
	private OrderGPSActivity.LocationHandler handler = new OrderGPSActivity.LocationHandler();
	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		
		mapLayout = new FrameLayout(this);
		mapView = new MapView(this, getString(R.string.map_api_key));
		mapLayout.addView(mapView);
		setContentView(mapLayout);
		
		setListener();
		canUpdate = true;
		String longitude = getIntent().getStringExtra(LocationInfoTableAdapter.LONGITUDE);
		String latitude = getIntent().getStringExtra(LocationInfoTableAdapter.LATITUDE);
		if(!longitude.trim().equals("") && !latitude.trim().equals("")){
			animateTo(Double.parseDouble(longitude), Double.parseDouble(latitude));
		}
		handler.sendEmptyMessage(handler.WHAT_LOADINGMAP_6);
	}
	
	@Override
	protected void onDestroy() {
		canUpdate = false;
		super.onDestroy();
	}
	
	private void setListener(){
		mapView.setClickable(true);
		mapCtr = mapView.getController();
		//默认缩放大小
		mapCtr.setZoom(15);
		//设置地图可以拖动和显示缩放组件
		mapView.setBuiltInZoomControls(true);
	}
	
	private static void animateTo(double longitude, double latitude){
		geoPoint = new GeoPoint((int)(latitude*1E6), (int)(longitude*1E6));
		//移动到该点
		mapCtr.animateTo(geoPoint);
		//以该点为中心显示在地图上
		mapCtr.setCenter(geoPoint);
		
		//将自定义层显示在地图上
		if(myol ==null){
			myol = new MyOverLay();
		}
		myol.onTap(geoPoint,mapView);
		List<Overlay> overlays = mapView.getOverlays();
		overlays.clear();
		overlays.add(myol);
		overlays = null;
	}
	
	public static class UpdateLocationHandler extends Handler{
		public final String LOCATION_KEY = "location_key";
		public final byte WHAT_UPDATE_LOCATION_0 = 0;
		
		@Override
		public void handleMessage(Message msg) {
			if(canUpdate){
				if(msg.what == WHAT_UPDATE_LOCATION_0){
					Bundle data = msg.getData();
					Location location = (Location)data.get(LOCATION_KEY);
					animateTo(location.getLongitude(), location.getLatitude());
				}
			}
		}
	}
	
	/**绘画定位点信息*/
	private static class MyOverLay extends Overlay{
		private Paint paint = new Paint();
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			paint.reset();
			
			//将地图上的点转换成屏幕上的像素点
			Point point = new Point();
			mapView.getProjection().toPixels(geoPoint, point);

			paint.setAntiAlias(true);
			//描画所在点
			paint.setStyle(Style.FILL);
			paint.setARGB(128, 0x33, 0x00, 0xFF);
			canvas.drawCircle(point.x, point.y, 5, paint);
			
			//描画范围圈
			paint.setStyle(Style.STROKE);
			paint.setARGB(64, 0x33, 0x00, 0xFF);
			
			float accuracy_radius = mapView.getProjection().metersToEquatorPixels(100);
			
			canvas.drawCircle(point.x, point.y,accuracy_radius , paint);
			paint.setStyle(Style.FILL);
			paint.setAlpha(8);
			canvas.drawCircle(point.x, point.y,accuracy_radius , paint);

			
		}
		
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	/**重新“返回键”的事件*/
	@Override
	public void onBackPressed() {
		Tool.backToMain(OrderGPSMapActivity.this);
	}
	
//	@Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		
//		if(keyCode == KeyEvent.KEYCODE_BACK){
//			Intent main = new Intent(OrderGPSMapActivity.this, OrderGPSActivity.class);
//			//main.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//			startActivity(main);
//		}
//		return false;
//	}

}
